<?php

namespace App\Http\Controllers;

use App\Models\Master;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class MasterController extends Controller
{

    public function upload() {
        $allow_type=["image/jpeg","image/gif","image/png"];
        if(!in_array($_FILES['upload_file']['type'],$allow_type)){
            $msg='图片类型不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['size']>2000000){
            $msg='图片大小不符合要求';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        elseif($_FILES['upload_file']['error']!=0){
            $msg='上传出错';
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        else {
            $src_image=$_FILES['upload_file']['tmp_name'];
            $size=getimagesize($_FILES['upload_file']['tmp_name']);
            $src_width=$size[0];
            $src_height=$size[1];
            $tar_width=400;
            $tar_height=$tar_width/$src_width*$src_height;
            $tar_thumb=imagecreatetruecolor($tar_width,$tar_height);
            if($_FILES['upload_file']['type']=='image/jpeg'){
                $src_thumb=imagecreatefromjpeg($src_image);
            }
            elseif ($_FILES['upload_file']['type']=="image/gif"){
                $src_thumb=imagecreatefromgif($src_image);
            }
            else{
                $src_thumb=imagecreatefrompng($src_image);
            }

            imagecopyresized($tar_thumb,$src_thumb,0,0,0,0, $tar_width,$tar_height,$src_width,$src_height);
            $path='./assets/images/expert_upload/'.$_FILES['upload_file']['name'];
            imagejpeg($tar_thumb,$path);
            return json_encode(['success'=>true,'photo'=>'/assets/images/expert_upload/'.$_FILES['upload_file']['name']]);
        }
    }
    public function create(){
        return view('admin.master-create');
    }


    public function store(Request $request){
        $data=$request->input();
        $rules=[
            'name'=>'required',
            'keshi'=>'required',
            'zhicheng'=>'required',
            'zhiwu'=>'required',
            'hospital'=>'required',
            'show'=>'required',
            'email'=>'required',
        ];
        $message=[

        ];
        $validate=Validator::make($data,$rules,$message);
        if ($validate->fails()){
            return view('admin.prompt');
        }else{
            $aMaster=$request->input();
            if(isset($aMaster['photo'])){
                $aMaster["thumb"]=$aMaster["photo"];
            }
            else{
                $aMaster["thumb"]='/assets/test_images/1.jpg';
            }
            Master::create($aMaster);
            return redirect('/adm/master-list');

        }


    }
    public function edit($id){
        $master=Master::find($id);
        return view('admin.master-edit')
            ->with('master',$master);
    }
    public function update(Request $request, $id)
    {
        $data = $request->input();
        $rules = [
            'name' => 'required',
            'keshi' => 'required',
            'zhicheng' => 'required',
            'zhuwu' => 'required',
            'hospital' => 'required',
            'show' => 'required',
            'email' => 'required',
        ];
        $message = [

        ];
        $validate = Validator::make($data, $rules, $message);
        $master=Master::find($id);
        $aMaster = $request->input();
        if(isset($aMaster['photo'])){
            $aMaster["thumb"]=$aMaster["photo"];
        }
        else{
            $aMaster["thumb"]=$master["thumb"];
        }
        $master->update($aMaster);
        return redirect('/adm/master-list');

    }
    public function delete($id){
        $master=Master::find($id);
        $master->delete();
        return redirect('/adm/master-list');

    }
}
